package com.atjg.autocat.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager

class NetworkChangeReceiver(private val onNetworkChangeListener: OnNetworkChangeListener) :
    BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (ConnectivityManager.CONNECTIVITY_ACTION == intent.action) {
            val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = cm.activeNetworkInfo
            if (networkInfo != null && networkInfo.isConnected) {
// 网络已连接
                onNetworkChangeListener.onNetworkConnected()
            } else {
// 网络已断开
                onNetworkChangeListener.onNetworkDisconnected()
            }
        }
    }

    interface OnNetworkChangeListener {
        fun onNetworkConnected()
        fun onNetworkDisconnected()
    }
}